import { withLoading } from 'utils/dva';
import services from 'services';
import model from '../model';
import { pageConfig } from 'configs/constants';

const defaultConfig = {
  pagination: {
    ...pageConfig,
    total: 0
  },
  list: []
};
const initDetails = {
  company: {},
  role: {},
  user: {}
};


export default model.extend ({

  namespace: 'lineData',
  state: {
    config: defaultConfig,
    user: [],
    details: initDetails,
    monitorData: {},
    equipList: [],
    templateList: [],
    initDetails,
    loading: {
      list: false,
      add: false,
      del: false
    }
  },

  subscriptions: {
    setup({ dispatch, history }) {
      // 监听路由的变化，请求页面数据
      return history.listen(({ pathname }) => {
        // let list = [];
        // if (pathname === '/user/userManage') {
        //   dispatch({ type: 'getList', payload: { list } });
        // }
      });
    }
  },

  effects: {
    * getEquipList({ payload }, { call, select, update }) {
      const { config: { pagination } } = yield select(({ lineData }) => lineData);
      const { pageSize, current } = pagination;
      const { data } = yield call(withLoading(services.equipment.getEquipmentList), 
      { ps: pageSize, pn: current, ...payload });
      yield update({ equipList: data.data });
    },
    // * getList({ payload }, { call, select, update }) {
    //   const { config: { pagination } } = yield select(({ equipData }) => equipData);
    //   const { pageSize, current } = pagination;
    //   const { data }  = yield call(withLoading(services.chart.getMonitorData, 'list'), 
    //   { ps: pageSize, pn: current, ...payload });
    //   yield update({ monitorData: data });
    // },
    * queryDataTemplateItems({ payload }, { call, update }) {
      const { data } = yield call(withLoading(services.chart.queryDataTemplateItems), { ...payload });
      yield update({ templateList: data });
    },
    * getMonitorData({ payload }, { call, update }) {
      const { data } = yield call(withLoading(services.chart.getMonitorData), { ...payload });
      yield update({ monitorData: data });
    }
    
  },

  reducers: {
    goPage(state, { payload }) {
      return {
        ...state,
        config: {
          ...state.config,
          pagination: {
            ...state.config.pagination,
            current: payload
          }
        }
      };
    },
    updateSearch(state, { payload: params }) {
      const { pn = 1 } = params;
      return {
        ...state,
        search: { ...state.search, ...params },
        config: {
          ...state.config,
          pagination: { ...state.config.pagination, current: pn }
        }
      };
    }
  }

});
